<?php
namespace app\index\controller;

use app\common\model\ArticleModel;
use app\common\model\SlideModel;
use think\Db;

class NewsController extends BaseController
{
    public function index()
    {
        $keyword = $this->request->param('keyword');
        $college_id = $this->request->param('college_id');

        $page = $this->request->param('page');
        $page = $page ? $page : 1;

        $time=time();

        $where=[
            ['lower_time','>',$time],['lower_time','=',0],'or'
        ];

        if ($keyword){
            $count = ArticleModel::where('status', 1)
                ->where('title','like','%'.$keyword.'%')
                ->whereOr('excerpt','like','%'.$keyword.'%')
                ->whereOr('content','like','%'.$keyword.'%')
                ->where($where)
                ->count();
            if (is_mobile()){
                $articles = ArticleModel::where('status', 1)
                    ->where('title','like','%'.$keyword.'%')
                    ->whereOr('excerpt','like','%'.$keyword.'%')
                    ->whereOr('content','like','%'.$keyword.'%')
                    ->where('lower_time','>',$time)
                    ->whereOr('lower_time','=',0)
                    ->field('id,title,excerpt,create_time,published_time')
                    ->order('create_time desc')
                    ->all();
            }else{
                $articles = ArticleModel::where('status', 1)
                    ->where('title','like','%'.$keyword.'%')
                    ->whereOr('excerpt','like','%'.$keyword.'%')
                    ->whereOr('content','like','%'.$keyword.'%')
                    ->where('lower_time','>',$time)
                    ->whereOr('lower_time','=',0)
                    ->field('id,title,excerpt,create_time,published_time')
                    ->order('create_time desc')
                    ->page($page,10)
                    ->all();
            }
        }else{
            $count = ArticleModel::where('status', 1)->count();
            if (is_mobile()){
                $articles = ArticleModel::where('status', 1)->field('id,title,excerpt,create_time,published_time,thumb')
                    ->order('create_time desc')
                    ->where('lower_time','>',$time)
                    ->whereOr('lower_time','=',0)
                    ->all();
            }else{
                $articles = ArticleModel::where('status', 1)->field('id,title,excerpt,create_time,published_time,thumb')
                    ->order('create_time desc')
                    ->where('lower_time','>',$time)
                    ->whereOr('lower_time','=',0)
                    ->page($page,10)
                    ->all();
            }
        }

        $newArticles = ArticleModel::where('status', 1)->field('id,title,excerpt,create_time,published_time,thumb')
            ->order('id desc')
            ->where('lower_time','>',$time)
            ->whereOr('lower_time','=',0)
            ->limit(0, 10)
            ->all();

        $this->assign([
            'count'=>$count,
            'articles'=>$articles,
            'news' => $newArticles,
            'page' => $page,
            'maxPage' => ceil($count / 10),
            'keyword' => $keyword,
            'college_id' => $college_id,
        ]);

        if (is_mobile()){
            $this->assign('maxPage',1);
            $this->assign('page',1);
            return $this->fetch('/mobile/news/index');

        }

        return $this->fetch();
    }


    public function detail(){
        $id = $this->request->param('id');
        if ($id){
            $news = ArticleModel::where('id',$id)->find();
            $news->setInc('hits',1);
            $news['content'] = htmlspecialchars_decode($news['content']);
            $this->assign('detail',$news);
            if (is_mobile()){
                return $this->fetch('/mobile/news/detail');
            }
            return $this->fetch();
        }else{
            $this->redirect('index');
        }
    }

    public function get_article_flow()
    {
        $res['code'] = 1;
        $res['msg'] = 'success';

        $count = ArticleModel::where('status', 1)->count();

        $page = $this->request->param('page');
        $articles = ArticleModel::where('status', 1)
            ->field('id,title,excerpt,create_time')
        ->order('id desc')
        ->limit($page * 10, 10)
        ->all();
        foreach ($articles as $key=>$vo){
            $articles[$key]['link'] = url('index/article/index','id='.$vo['id']);
        }
        $res['data'] = $articles;
        $res['pages'] = $count / 10;
        return $res;
    }

    /**
     * 联系我们
     * @return mixed
     */
    public function contact()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $res['code'] = 1;
            $res['msg'] = '感谢您的留言！';

            $data['create_time'] = date('Y-m-d H:i:s',time());

            $result = Db::name('contact')->insert($data);

            if ($result === false) {
                $res['code'] = 0;
                $res['msg'] = '添加失败！';
            }

            return json($res);
        }

        return $this->fetch('/contact');
    }
}
